package cn.itcast.controller;

import cn.itcast.domain.Order;
import cn.itcast.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Resource
    private UserService service;

    @RequestMapping(value = "/addOrder.do", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Map<String, Object> addOrder(Integer cid, String address, Integer paymentstatus, Integer price) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date());
        Map<String, Object> maplist = new HashMap<>();
        String oid = date.substring(8, 10) + cid;
        String result = null;
        Boolean flag = service.addorder(cid, Integer.valueOf(oid), date, paymentstatus, address, price);
        if (flag) {
            result = "1";
        } else {
            result = "0";
        }
        maplist.put("result", result);
        return maplist;
    }

    @RequestMapping("/findOrder.do")
    @ResponseBody
    public List<Order> findOrder() {
        List<Order> list = service.findorder();
        return list;
    }

    @RequestMapping("/findOrderByCid.do")
    @ResponseBody
    public Order findOrderByCid(Integer cid) {
        Order order = service.findOrderByCid(cid);
        System.out.println(order);
        return order;
    }
}
